package main;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import myEddy.operator.Join;
import myEddy.operator.Select;

@SuppressWarnings("serial")
public class fram_pan1 extends JPanel {
	
	JLabel l1,l2,l3,l4,l5,l6,lp1,lp2,lp3, lp4;
	   JTextField ttable1, tchamp1, tchamp2,text4,text5,text6;
	   JLayeredPane  layeredMethode;
	   JRadioButton bselect,bJoin;
		JButton ajouter;
		
fram_pan1(){
	
	
	
	setLayout( null );
	setBackground(new Color(200,200,242));
	setBounds(1, 2, 220,fram.f.getHeight()-6);
	setBorder(BorderFactory.createLineBorder(Color.black, 2));
	
	
    
     layeredMethode=new JLayeredPane();
	 layeredMethode.setBounds(5,100,210,650);
	 layeredMethode.setBorder(BorderFactory.createTitledBorder(
               "Definir La Requete:"));
	 layeredMethode.setBackground(Color.white);
 	 bselect=new JRadioButton("Selection");
 	 bselect.setBounds(5,20,150,30);
 	 layeredMethode.add(bselect);
 	
 	 bJoin=new JRadioButton("Jointure");
	 bJoin.setBounds(5,60,150,30);
	 layeredMethode.add(bJoin);
	 ButtonGroup groupe2 = new ButtonGroup();
	 groupe2.add(bselect);
	 groupe2.add(bJoin);
	 
	
	 l1=new JLabel("Nom Table 1");
	 l1.setBounds(5, 100, 150, 30);
	 layeredMethode.add(l1);
	 ttable1=new JTextField();
	 ttable1.setBounds(15, 140, 150, 40);
	 layeredMethode.add(ttable1);
	 l1.setVisible(false); ttable1.setVisible(false);
    
	 l2=new JLabel();
	 l2.setBounds(5, 190, 150, 30);
     tchamp1=new JTextField();
     tchamp1.setBounds(15, 230, 150, 40);
     l3=new JLabel();
	 l3.setBounds(5, 280, 150, 30);
     tchamp2=new JTextField();
     tchamp2.setBounds(15, 310, 150, 40);
     layeredMethode.add(l2);layeredMethode.add(tchamp1);layeredMethode.add(l3);
     layeredMethode.add(tchamp2);
     l2.setVisible(false); tchamp1.setVisible(false);
     l3.setVisible(false); tchamp2.setVisible(false);
     
     l4=new JLabel();
     l4.setBounds(5, 360, 150, 30);
     text4=new JTextField();
     text4.setBounds(15, 400, 150, 40);
     layeredMethode.add(l4);layeredMethode.add(text4);
     l4.setVisible(false); text4.setVisible(false);
     l5=new JLabel();
     l5.setBounds(5, 450, 150, 30);
   
     layeredMethode.add(l5);
     l5.setVisible(false);
     add(layeredMethode);
     ajouter=new JButton("Add");
     ajouter.setBounds(50, 600, 100, 40);
     layeredMethode.add(ajouter);
     
     ajouter.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if (bselect.isSelected()){	
				
				    String N_table1=ttable1.getText();
					String N_Colon=tchamp1.getText();
					String N_Valeur=tchamp2.getText();
					System.out.println(""+N_table1);
					if(tchamp1.getText().isEmpty()==false && tchamp2.getText().isEmpty()==false 
					     && ttable1.getText().isEmpty()==false){
					fram.E.addOperator(new Select(N_table1,N_Colon,N_Valeur));
					fram.listereq.add("Select from "+N_table1+" where"+" "+N_Colon+" == "+N_Valeur+";");
					
						 
					}else{
					JOptionPane.showMessageDialog(null,"vous devez remplir tous les champs"
					        ," ERROR ",JOptionPane.ERROR_MESSAGE);
							
					    }	
					
					
				}else{
					if (bJoin.isSelected()){	
						 String N_table1=ttable1.getText();
							String N_table2=tchamp1.getText();
							String N_Colon1=tchamp2.getText();
							String N_Colon2=text4.getText();
							System.out.println(""+N_table1+" "+N_table2);
				    if(tchamp1.getText().isEmpty()==false && tchamp2.getText().isEmpty()==false 
				    		&& text4.getText().isEmpty()==false && ttable1.getText().isEmpty()==false){
					        fram.E.addOperator(new Join(N_table1,N_table2,N_Colon1,N_Colon2));
					        fram.listereq.add("Join "+N_table1+" et "+N_table2+" where "+N_Colon1+" =="+N_Colon2);
				    }else{
				    	JOptionPane.showMessageDialog(null,"vous devez remplir tous les champs"
				        ," ERROR ",JOptionPane.ERROR_MESSAGE);
						
				    }	}
				}}
    	 
     });
    
    bselect.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			 ttable1.setVisible(true);
			 tchamp1.setVisible(true);tchamp2.setVisible(true);
			 l2.setText("Nom Colone");l3.setText("Valeur");				   
			 l1.setVisible(true);l2.setVisible(true);l3.setVisible(true);
			 text4.setVisible(false);l4.setVisible(false);
			 repaint();
			 ttable1.setText("");tchamp1.setText("");tchamp2.setText("");text4.setText("");
			
			 
		}
 		 
 	 });
   
    
    bJoin.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent arg0) {
		
			ttable1.setVisible(true);
			 l2.setText("Nom table2");
			   l3.setText("Nom COlone1");
			   l4.setText("Nom COlone2");
			  
			 l1.setVisible(true);l2.setVisible(true);l3.setVisible(true);
			 tchamp1.setVisible(true);tchamp2.setVisible(true);
			 text4.setVisible(true);l4.setVisible(true);
			 ttable1.setText("");tchamp1.setText("");tchamp2.setText("");text4.setText("");
			 
				repaint();
		}
 		 
 	 });
	    repaint();
	
}
}
